/**
 * @(#)ProvinceServiceImpl.java 2017年6月1日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.domian.IpRange;
import org.jsbd.boss.domian.TPhoneAreaProvinceInfo;
import org.jsbd.boss.service.IIpService;
import org.jsbd.boss.service.IProvinceService;
import org.jsbd.boss.util.PhoneUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.qy.recphone.dal.domain.ResultInfo;
import com.qy.recphone.service.IRecPhoneService;

/**
 * 省份信息
 *
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class ProvinceServiceImpl implements IProvinceService {
    public static final Logger LOGGER = LoggerFactory.getLogger(ProvinceServiceImpl.class);

    @Resource
    private IIpService ipService;
    @Resource
    private IRecPhoneService recPhoneService;

    /*
     * (non-Javadoc)
     *
     * @see
     * org.jsbd.boss.service.IProvinceService#getProvinceInfo(java.lang.String)
     */
    @Override
    public IpRange getProvinceInfo(String imsi, String mobile) {
        IpRange ipRange = new IpRange();
        try {
            mobile = TaskUtil.isMobileNO(mobile);
            ResultInfo resultInfo = recPhoneService.getphoneAndProvinceByImsi(imsi);
            if (resultInfo != null && resultInfo.getProvinceId() != null) {
                String phoneNum = resultInfo.getPhoneNum();
                if (StringUtils.isNotBlank(phoneNum)) {
                    String mobileNO = TaskUtil.isMobileNO(phoneNum);
                    if (mobileNO != null) {
                        ipRange.setPhoneNum(mobileNO);
                    }else{
                        if(mobile != null){
                            ipRange.setPhoneNum(mobile);
                        }
                    }
                }
                ipRange.setCityId(resultInfo.getCityId());
                ipRange.setProvinceId(resultInfo.getProvinceId());
            } else {
                TPhoneAreaProvinceInfo info = getAreaByImsi(imsi, mobile);
                if (info != null && info.getProvinceId() != null) {
                    ipRange.setProvinceId(Integer.parseInt(info.getProvinceId()));
                    ipRange.setCityId(info.getCityId());
                    ipRange.setPhoneNum(mobile);
                    return ipRange;
                }else{
                    ipRange.setProvinceId(-1);
                    ipRange.setCityId(-1);
                    ipRange.setPhoneNum(mobile);
                }
            }
        } catch (Exception e) {
            LOGGER.error("获取手机号和省份失败 {} {}", new Object[]{imsi, mobile , e});
            ipRange.setPhoneNum(mobile);
            ipRange.setProvinceId(-1);
            ipRange.setCityId(-1);
        }
        return ipRange;

    }

    private TPhoneAreaProvinceInfo getAreaByImsi(String imsi, String mobile) {
        String phoneArea = null;
        if(mobile != null){
            phoneArea = mobile.substring(0,7);
        }else{
            phoneArea = PhoneUtil.getMobileByIMSI(imsi);
        }
        if (StringUtils.isBlank(phoneArea) || phoneArea.length() < 7) {
            return null;
        }
        return ipService.getUserLocationByPhoneArea(phoneArea);
    }
}
